<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/26
 * Time: 17:13
 */
namespace App\FreeBird\Repositories;

use App\FreeBird\Contracts\AutomaticInterface;
use App\FreeBird\Models\AutomaticModel;

class  DbAutomaticRepositories implements AutomaticInterface
{
    /**
     * 自动提交
     * @return AutomaticModel
     */
    public function getAutomaticModel(){
        return app()->make(AutomaticModel::class);
    }

    /**
     * 接单模型
     * @return mixed
     */
    public function getInfoAcceptlogModel(){
        return app()->make(InfoAcceptlogModel::class);
    }

    /**
     * 创建订单
     * @param $data
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function add($data){
         return $this->getAutomaticModel()->newQuery()->updateOrCreate(["uid"=>AUID],["uid"=>AUID]);
    }

    /**
     * 取消订单
     * @param $where
     * @return mixed
     */
    public function cancel($where){

        return $this->getAutomaticModel()->newQuery()->delete($where);
    }

    /**
     * 自动接单
     * @param \App\FreeBird\Contracts\订单对象 $orderInfo
     * @return mixed|void
     */
    public function autoAcceptOrder($orderInfo){

        $autoInfo=$this->getAutomaticModel()->newQuery()->whereNotIn("uid",[$orderInfo->uid])->first();
        if($autoInfo){
            $data=array();
            $data["order_sn"]=$orderInfo->order_sn;
            $data["uid"]=$orderInfo->uid;
            $data["buid"]=$autoInfo->uid;
            if($this->getInfoAcceptlogModel()->newQuery()->create($data)){
                    $this->getAutomaticModel()->newQuery()->where("id",$autoInfo->id)->delete();
                    return $autoInfo->uid;
            }

        }
    }
}